<?php
/**
 * minf - core library
 * modules/url.php
 *
 * @author per function || minf.org team
 * @version 0.beta4
 * @copyright no,no,left, minf.org, 25 February, 2009
 * @license GPL
 * @package default
 **/

/**
 * generate pcre-string and varlist for Minf.URL Schemas
 *
 * @param String $schema Minf URL Schema
 * @param Boolean $novars Returns just pcre instead of array (if true)
 * @return Array (pcre, vars) or String $pcre if ($novars)
 */

function schema2pcre($schema, $novars = false) {
	$matchcode = "[\:\s\%\@\.\=a-z0-9_-]";
	$matchcodewithslash = "[\:\s\%\@\/\.\=a-z0-9_-]";
	$pcre = "/^\/";
	$prsq = false;
	$prug = false;
	$nmgt = false;
	$varn = "";
	$vart = "";
	$hookfrom = -1;
	$vars = array();
	$charcount = 0;
	$withslashes = false;

	for ($pos=0;$pos<strlen($schema);$pos++) {
		if ($hookfrom!=-1) $chars = substr($schema, $hookfrom, $pos-$hookfrom); else $chars = "";
		$char = substr($schema, $pos, 1);

		if ($char=="{") {
			/*if (!$prug&&!$prsq) {
				if ($charcount>0) {
					$pcre .= "){1}";
					$charcount=0;
				}
			}*/
			if (substr($schema, $pos+1, 1)=="!") {
				$withslashes = true;
				$pos++;
			}
			$hookfrom = $pos+1;
			$nmgt = true;
			$prug = true;
			continue;
		}elseif ($char=="}") {
			$prug = false;
			if ($nmgt) {
				$varn = $chars;
				$vars[$varn] = "";
				$nmgt = false;
				$hookfrom = -1;
				//(?<lang>[a-z0-9_-]+)
				//$pcre .= "(?<{$varn}>[\:\w\s-]+){1}";
				if ($withslashes) $pcre .= "(?P<{$varn}>{$matchcodewithslash}+){1}";
				else $pcre .= "(?P<{$varn}>{$matchcode}+){1}";
				$withslashes = false;
			}
			continue;
		}elseif ($char=="[") {
			/*if (!$prug&&!$prsq) {
				if ($charcount>0) {
					$pcre .= "){1}";
					$charcount=0;
				}
			}*/
			$prsq = true;
			$pcre .= "(";
			continue;
		}elseif ($char=="]") {
			$prsq = false;
			$pcre .= ")?";
			continue;
		}elseif ($char==":") {
			if ($nmgt) {
				$varn = $chars;
				$delimiter = substr($schema, $pos+1, strpos($schema, "}", $pos)-$pos-1);
				$vars[$varn] = $delimiter;
				$nmgt = false;
				$hookfrom = -1;
				//(?<-varname->(([a-z0-9_-]+)\/)*$)
				$delimiter = str_replace("/", "\/", $delimiter);
//				$pcre .= "(?<{$varn}>(([\:\w\s-]+)$delimiter)*){1}";
				if ($withslashes) $pcre .= "(?P<{$varn}>(({$matchcodewithslash}+)$delimiter)*){1}";
				else $pcre .= "(?P<{$varn}>(({$matchcode}+)$delimiter)*){1}";
				$withslashes = false;
				continue;
			}else{
				$pcre .= $char;
				continue;
			}
		}else{
			if (!$prug) {
				if ($char=="/") $char = "\/";
				//if ($charcount==0&&!$prsq&&!$prug) $pcre .= "(?:";
				$pcre .= $char;
				//if (!$prsq&&!$prug) $charcount++;
				continue;
			}
		}
	}
	/*if (!$prug&&!$prsq&&$charcount>0) {
			$pcre .= "){1}";
			$charcount=0;
	}*/
	$pcre .= "/iuxS";
	return ($novars) ? $pcre : array($pcre, $vars);
}

/**
 * parse a URL using data returned from schema2pcre
 *
 * @param String $url linkreq returned from user_request()
 * @param Array $s2p data returned from schema2pcre
 * @return Array variables grabbed from url using pcre
 */
function parseUrl($url, $s2p) {
	list($pcre, $vars) = $s2p;
	if (substr($url,-1)!="/") $url .= "/";
	if (substr($url,0,1)!="/") $url = "/".$url;	

	if (!preg_match_all($pcre, $url, $urlmatch)) return false;

	foreach ($vars as $vn => $vr) {
		if ($vr=="") {
			$data[$vn] = $urlmatch[$vn][0];
		}else{
			$data[$vn] = explode($vr, $urlmatch[$vn][0]);
			foreach ($data[$vn] as $n => $k) if (trim($k)=="") unset($data[$vn][$n]);
		}
	}

	return $data;
}

function formAppCall() {
	global $urlconf, $urldata;
	//print_r($urlconf);
	$appch = explodetrim("||", $urlconf["application"]);
	foreach ($appch as $app) {
		foreach ($urldata as $d=>$v) if (is_string($v)) $app = str_replace("{".$d."}", $v, $app);
		if (strlen(trim($app))>0) break;
	}
	$callch = explodetrim("||", $urlconf["method"]);
	foreach ($callch as $call) {
		foreach ($urldata as $d=>$v) if (is_string($v)) $call = str_replace("{".$d."}", $v, $call);
		if (strlen(trim($call))>0) break;
	}
	/**/
	$langch = explodetrim("||", $urlconf["language"]);
	foreach ($langch as $lng) {
		foreach ($urldata as $d=>$v) if (is_string($v)) $lng = str_replace("{".$d."}", $v, $lng);
		if (strlen(trim($lng))>0) break;
	}
	/**/
	$params = array();
	if (!is_array($urlconf["params"])) $urlconf["params"] = array($urlconf["params"]);
	foreach ($urlconf["params"] as $param) {
		$paramsch = explodetrim("||", $param);
		foreach ($paramsch as $p) {
			foreach ($urldata as $d=>$v) 
				if (is_string($v)&&strpos($p, "{".$d."}")!==false) $p = str_replace("{".$d."}", $v, $p);
				elseif (is_array($v)&&strpos($p, "{:".$d."}")!==false) {
					$params[] = $v;
					$p = str_replace("{:".$d."}", "", $p);
				}elseif (is_array($v)&&strpos($p, "{".$d."}")!==false) {
					foreach ($v as $_add) $params[] = $_add;
					$p = str_replace("{".$d."}", "", $p);
				}
			if (is_string($p)&&strlen(trim($p))>0) $params[] = $p;
		}
		
	}
	return array("app"=>$app, "call"=>$call, "argv"=>$params, "argc"=>sizeof($params), "lang"=>$lng);
}

function redirect($url) {
	header("location: $url");
}

function redirectr($url) {
	global $root;
	redirect($root.$url);
}

?>